﻿using Craftsman.Core.CustomizeException;
using Craftsman.Core.Domain;
using Craftsman.Core.Domain.Repositories;
using Craftsman.xCoronavirus.Domain.Contract;
using Craftsman.xCoronavirus.Domain.Dto;
using Craftsman.xCoronavirus.Domain.Entities;
using Craftsman.xCoronavirus.Domain.Repositories;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Craftsman.xCoronavirus.Domain
{
    public interface IModifyRequestService : IService
    {
        List<ModifyRequestHospitalDTO> GetModifyRequests(string HospitalName, string City, ModifyRequestStatus[] Status);
        ModifyRequest GetModifyRequest(string id);
        void CreateModifyRequest(ModifyRequest entity);
        void PassModifyRequest(string id);
        void InvalidModifyRequest(string id);

    }
    public class ModifyRequestService : IModifyRequestService
    {
        private IModifyRequestRepository _repoModifyRequest;
        private IHospitalRepository _repoHospital;
        public ModifyRequestService(
            IModifyRequestRepository repoModifyRequest,
            IHospitalRepository repoHospital
        )
        {
            _repoModifyRequest = repoModifyRequest;
            _repoHospital = repoHospital;
        }
        public ModifyRequest GetModifyRequest(string id)
        {
            var entity = _repoModifyRequest.GetModifyRequest(id);
            if(entity == null)
            {
                throw new BusinessException($"不存在对应提交者的请求：id=<{id}");
            }
            return entity;
        }

        public List<ModifyRequestHospitalDTO> GetModifyRequests(string HospitalName, string City, ModifyRequestStatus[] Status)
        {
            var mrQuery = _repoModifyRequest.GetAll();
            if (Status != null && Status.Length > 0)
                mrQuery = mrQuery.Where(r => Status.Contains(r.Status));
            var hpQuery = _repoHospital.GetAll().Where(h => EF.Functions.Like(h.Name, $"%{HospitalName}%") && EF.Functions.Like(h.City, $"%{City}%"));

            var aa = from mr in mrQuery
                     join hp in hpQuery on mr.HospitalId equals hp.Id

                     select new ModifyRequestHospitalDTO
                     {
                         Id = mr.Id,
                         HospitalId = mr.HospitalId,
                         CreatedTime = mr.CreatedTime,
                         Requester = mr.Requester,
                         RequesterPhone = mr.RequesterPhone,
                         Status = mr.Status,
                         Code = hp.Code,
                         City = hp.City,
                         HospitalName = hp.Name,
                         StaffCount = hp.StaffCount,
                         Address = hp.Address
                     };
            return aa.ToList();
        }


        public void CreateModifyRequest(ModifyRequest entity)
        {
            _repoModifyRequest.CreatModifyRequest(entity);
        }

        public void PassModifyRequest(string id)
        {
            //作废的只改状态
            var entity = _repoModifyRequest.GetModifyRequest(id);
            entity.Status = ModifyRequestStatus.Pass;
            _repoModifyRequest.UpdateModifyRequest(entity);
        }
        public void InvalidModifyRequest(string id)
        {
            //此处需要做一些验证
            var entity = _repoModifyRequest.GetModifyRequest(id);
            if (entity == null)
            {
                throw new BusinessException("申请单不存在");
            }
            else if (entity.Status != ModifyRequestStatus.Draft)
            {
                throw new BusinessException("申请单不是未审批状态");
            }
            var hospital = _repoHospital.GetHospital(entity.HospitalId);
            //此处需要修改联系人信息
            foreach (var contactItem in entity.Contacts)
            {
                var hosContact = hospital.Contacts.FirstOrDefault(c => c.Id == contactItem.ContactId);
                if (hosContact != null)
                {
                    hosContact.HospitalContact = contactItem.HospitalContact;
                    hosContact.Phone = contactItem.Phone;
                    // hosContact.Wechat = contactItem.Wechat; 医院的联系人表没有wechat
                    hosContact.Remark = contactItem.Remark;
                }
            }
            //此处需要修改物品信息
            foreach (var materialDemandItem in entity.MaterialDemands)
            {
                var hosDemand = hospital.MaterialDemands.FirstOrDefault(c => c.Id == materialDemandItem.DemandId);
                if (hosDemand != null)
                {
                    hosDemand.Quantity = materialDemandItem.Quantity;
                    hosDemand.Unit = materialDemandItem.Unit;
                    hosDemand.Level = materialDemandItem.Level;
                    hosDemand.Remark = materialDemandItem.Remark;
                }
            }

            entity.Status = ModifyRequestStatus.Invalid;
            _repoModifyRequest.UpdateModifyRequest(entity);
            _repoHospital.UpdateHospital(hospital);
        }


        

    }
}

